package o;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Iterator;

public final class fk implements Iterable<Intent> {
    private final Context ˎ;
    private final ArrayList<Intent> ˏ = new ArrayList();

    public interface e {
        @Nullable
        Intent ॱॱ();
    }

    private fk(Context context) {
        this.ˎ = context;
    }

    @NonNull
    public static fk ˏ(@NonNull Context context) {
        return new fk(context);
    }

    @NonNull
    public fk ˎ(@NonNull Intent intent) {
        this.ˏ.add(intent);
        return this;
    }

    @NonNull
    public fk ˊ(@NonNull Activity activity) {
        Intent ˋ;
        Intent intent = null;
        if (activity instanceof e) {
            intent = ((e) activity).ॱॱ();
        }
        if (intent == null) {
            ˋ = fg.ˋ(activity);
        } else {
            ˋ = intent;
        }
        if (ˋ != null) {
            ComponentName component = ˋ.getComponent();
            if (component == null) {
                component = ˋ.resolveActivity(this.ˎ.getPackageManager());
            }
            ˏ(component);
            ˎ(ˋ);
        }
        return this;
    }

    public fk ˏ(ComponentName componentName) {
        int size = this.ˏ.size();
        try {
            Intent ˋ = fg.ˋ(this.ˎ, componentName);
            while (ˋ != null) {
                this.ˏ.add(size, ˋ);
                ˋ = fg.ˋ(this.ˎ, ˋ.getComponent());
            }
            return this;
        } catch (Throwable e) {
            Log.e("TaskStackBuilder", "Bad ComponentName while traversing activity parent metadata");
            throw new IllegalArgumentException(e);
        }
    }

    @Deprecated
    public Iterator<Intent> iterator() {
        return this.ˏ.iterator();
    }

    public void ˋ() {
        ˎ(null);
    }

    public void ˎ(@Nullable Bundle bundle) {
        if (this.ˏ.isEmpty()) {
            throw new IllegalStateException("No intents added to TaskStackBuilder; cannot startActivities");
        }
        Intent[] intentArr = (Intent[]) this.ˏ.toArray(new Intent[this.ˏ.size()]);
        intentArr[0] = new Intent(intentArr[0]).addFlags(268484608);
        if (!fo.ˊ(this.ˎ, intentArr, bundle)) {
            Intent intent = new Intent(intentArr[intentArr.length - 1]);
            intent.addFlags(268435456);
            this.ˎ.startActivity(intent);
        }
    }
}
